//
//  SDAccountInfoManager.m
//  SKinDiary
//
//  Created by pcyang on 16/7/14.
//  Copyright © 2016年 tencent. All rights reserved.
//

#import "SDAccountInfoManager.h"

static NSString *const kAccountSessionId = @"kAccountSessionId";
static NSString *const kAccountUserId = @"kAccountUserId";
static NSString *const kAccountNickName = @"kAccountNickName";
static NSString *const kAccountAvatarUrl = @"kAccountAvatarUrl";
static NSString *const kAccountLoginType = @"kAccountLoginType";
static NSString *const kAccountOldUser = @"kAccountOldUser";

static NSString *const kAccountMobileNumber = @"kAccountMobileNumber";
static NSString *const kAccountPswd = @"kAccountPswd";

@implementation SDAccountInfoManager
static SDAccountInfoManager *accountInstance;

+(SDAccountInfoManager*)sharedInstance
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken,
                  ^{
                      accountInstance  = [[super allocWithZone:nil]  init];
                      
                      NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
                      
                      accountInstance.sessionId = [userDefaults objectForKey:kAccountSessionId];
                      accountInstance.userId = [userDefaults objectForKey:kAccountUserId];
                      accountInstance.nickName = [userDefaults objectForKey:kAccountNickName];
                      accountInstance.avatarUrl = [userDefaults objectForKey:kAccountAvatarUrl];
                      accountInstance.mobileNumber = [userDefaults objectForKey:kAccountMobileNumber];
                      accountInstance.password = [userDefaults objectForKey:kAccountPswd];
                      
                  });
    return accountInstance ;
}

-(void)saveAccount
{
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    
    if (CHECK_VALID_STRING(accountInstance.sessionId))
        [userDefaults setObject:accountInstance.sessionId forKey:kAccountSessionId];
    else
        [userDefaults removeObjectForKey:kAccountSessionId];
    
    if (CHECK_VALID_STRING(accountInstance.userId))
        [userDefaults setObject:accountInstance.userId forKey:kAccountUserId];
    else
        [userDefaults removeObjectForKey:kAccountUserId];
    
    if (CHECK_VALID_STRING(accountInstance.nickName))
        [userDefaults setObject:accountInstance.nickName forKey:kAccountNickName];
    else
        [userDefaults removeObjectForKey:kAccountNickName];
    
    if (CHECK_VALID_STRING(accountInstance.avatarUrl))
        [userDefaults setObject:accountInstance.avatarUrl forKey:kAccountAvatarUrl];
    else
        [userDefaults removeObjectForKey:kAccountAvatarUrl];
    
    if (CHECK_VALID_STRING(accountInstance.mobileNumber))
        [userDefaults setObject:accountInstance.mobileNumber forKey:kAccountMobileNumber];
    else
        [userDefaults removeObjectForKey:kAccountMobileNumber];
    
    if (CHECK_VALID_STRING(accountInstance.password))
        [userDefaults setObject:accountInstance.password forKey:kAccountPswd];
    else
        [userDefaults removeObjectForKey:kAccountPswd];
    
//    if (CHECK_VALID_STRING(accountInstance.sessionId) && CHECK_VALID_STRING(accountInstance.userId))
//        [userDefaults setInteger:accountInstance.loginType forKey:kAccountLoginType];
    
    //   if (CHECK_VALID_STRING(accountInstance.userId))
    //       [userDefaults removeObjectForKey:accountInstance.userId];
    
    [userDefaults synchronize];
}

-(void)clearAccount
{
    accountInstance.sessionId = nil;
    accountInstance.userId = nil;
    accountInstance.nickName = nil;
    accountInstance.avatarUrl = nil;
    accountInstance.mobileNumber = nil;
    accountInstance.password = nil;
    [self saveAccount];
}

@end
